package com.threeSales.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.threeSales.entity.User;
import com.threeSales.services.UserService;
import com.threeSales.utils.JacksonJsonUtil;
import com.threeSales.utils.Uuid;

@Controller
@RequestMapping("/user")
public class UserController {
	
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/addUser")
	public void addUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
		int name = Integer.parseInt(request.getParameter("name").trim());
		String pwd = request.getParameter("pwd");
		String id = Uuid.getUUID();
		String fid = "0";
		User user = new User(id,name,pwd,fid);
		String result = userService.userAdd(user,id);
		String resultJson = JacksonJsonUtil.beanToJson(result);
		PrintWriter printWriter = response.getWriter();
		printWriter.write(resultJson);
		printWriter.flush();
		printWriter.close();
	}
	
	@RequestMapping("/userAdd")
	public void userAdd(HttpServletRequest request,HttpServletResponse response) throws Exception{
		int name = Integer.parseInt(request.getParameter("name").trim());
		String pwd = request.getParameter("pwd");
		String fuid = request.getParameter("fuid");
		String id = Uuid.getUUID();
		User user = new User(id,name,pwd,fuid);
		String result = userService.addUser(user,fuid,id);
		String resultJson = JacksonJsonUtil.beanToJson(result); 
		PrintWriter printWriter = response.getWriter();
		printWriter.write(resultJson);
		printWriter.flush();
		printWriter.close();
		
	}

}
